﻿app.controller('SafetyCertificateController', function ($scope, $http, $window, $compile, $filter, ipCookie, common, option, Upload) {
    $scope.result = {};
    $scope.dateOption = option.date;
    $scope.datetimeOption = option.datetime;

    $scope.BindDepartmentList = function (control, defaultvalue) {
        $scope.PageLoading = true;
        $http({
            method: 'POST',
            url: '../Organization/GetDepartmentList'
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (n.DepartmentName != "系统支持") {
                            if (defaultvalue != null) {
                                if (n.Id == defaultvalue)
                                    $("#" + control).append($compile("<option selected='selected' value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                                else
                                    $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                            else {
                                $("#" + control).append($compile("<option value=\"" + n.Id + "\">" + n.DepartmentName + "</option>")($scope));
                            }
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }


    $scope.GetAllUserList = function (control, defaultvalue, departmentId) {
        $scope.PageLoading = true;
        $("#" + control).empty();
        $http({
            method: 'POST',
            url: '../Organization/GetUserListByDepartment',
            data: { 'DepartmentId': departmentId }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                if (ret.Code == 100) {
                    $.each(ret.Data, function (i, n) {
                        if (defaultvalue != null) {
                            if (defaultvalue.includes(n.Id + ""))
                                $("#" + control).append($compile("<option selected='selected' value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                            else
                                $("#" + control).append($compile("<option value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                        }
                        else {
                            $("#" + control).append($compile("<option value=\"" + n.UserNo + "\">" + n.UserName + "-" + n.UserNo + "</option>")($scope));
                        }
                    })
                    $("#" + control).selectpicker('refresh');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.AddJobCertificate = function (model) {
        var files = document.querySelector('input[type=file]').files;
        Upload.upload({
            method: 'POST',
            url: '../SafetyCertificate/AddJobCertificate',
            data: { 'model': model },
            file: files
        })
            .success(function (ret) {
                if (ret.Code == 100) {
                    alertify.alert("从业资证添加成功！");
                    setTimeout(function () {
                        location.href = "JobCertificatePageList?page=1";
                    }, 2000)
                } else
                    alertify.alert(ret.Message);
            })
            .error(function (data, status, headers, config) {
                common.error($scope);
            })
    }

    $scope.GetJobCertificatePageList = function (model, page) {
        $scope.DataLoading = true;
        $scope.DataList = [];
        $http({
            method: 'POST',
            url: '../SafetyCertificate/GetJobCertificatePageList',
            data: { "model": model },
            params: { 'page': page }
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.TotalCount = ret.Data.TotalItemCount;
                    $scope.DataList = ret.Data.Data;
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }

    $scope.GetJobCertificateDetail = function (Id, v) {
        $scope.DataLoading = true;
        $scope.DataModel = {};
        $http({
            method: 'POST',
            url: '../SafetyCertificate/GetJobCertificateDetail',
            data: { "Id": Id },
        })
            .success(function (ret) {
                common.success($scope, null, ret);
                $scope.DataLoading = false;
                if (ret.Code == 100) {
                    $scope.DataModel = ret.Data;
                    $scope.DataModel.CreateTime = $filter('JSONDate')($scope.DataModel.CreateTime, 'yyyy-MM-dd HH:mm:ss');
                }
                else {
                    alertify.alert(ret.Msg);
                }
            })
            .error(function (data, status, headers, config) {
                alertify.alert("请检查网络");
            });
    }


    $scope.DeleteJobCertificateDetail = function (Id, model, page) {
        alertify.okBtn("确认")
            .cancelBtn("取消").confirm("确认删除该数据？", function (ev) {
                ev.preventDefault();
                $http({
                    method: 'POST',
                    url: '../SafetyCertificate/DeleteJobCertificateDetail',
                    data: { 'Id': Id },
                })
                    .success(function (ret) {
                        $scope.DataLoading = false;
                        if (ret.Code == 100) {
                            alertify.alert(ret.Msg);
                            setTimeout(function () {
                                $scope.GetJobCertificatePageList(model, page);
                            }, 2000);
                        } else
                            alertify.alert(ret.Msg);
                    })
                    .error(function (data, status, headers, config) {
                        common.error($scope);
                        $scope.PageLoading = false;
                        alertify.alert("请检查网络");
                    })
            }, function (ev) {
                ev.preventDefault();

            });
    }
});